//
//  DDHomeRecommendBannerView.m
//  DYD
//
//  Created by 邱明 on 2018/2/22.
//  Copyright © 2018年 Diyidan. All rights reserved.
//

#import "DDHomeRecommendBannerView.h"
#import "iCarousel.h"
//#import "DDHomeSmallBannerView.h"
//#import "NSTimer+Weak.h"
#import "DDHomeBannerPageControlView.h"
//#import "UIImageView+EMWebCache.h"
#import "UIImageView+WebCache.h"

@interface DDHomeRecommendBannerView ()<iCarouselDelegate, iCarouselDataSource, DDHomeBannerPageControlViewDataSource>

/** 大banner的高度 */
@property (nonatomic, assign) CGFloat bigBannerHeight;
/** 大banner小banner的间隙 */
@property (nonatomic, assign) CGFloat bannerGap;
/** 小banner的高度 */
@property (nonatomic, assign) CGFloat smallBannerHeight;
/** 小banner的宽度(每个) */
@property (nonatomic, assign) CGFloat smallBannerItemWidth;
/** 小banner之间的间隙 */
@property (nonatomic, assign) CGFloat smallBannerItemGap;

/** 大banner */
@property (nonatomic, strong) iCarousel *bigBanner;
/** 大banner数组 */
@property (nonatomic, copy) NSArray <NSString *>*bigPromotions;
/** 小圆点 */
@property (nonatomic, strong) DDHomeBannerPageControlView *bigBannerPageControl;

@end

@implementation DDHomeRecommendBannerView

+ (instancetype)homeRecommendBannerView {
    DDHomeRecommendBannerView *bannerView = [[DDHomeRecommendBannerView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 0)];
    return bannerView;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self configerBannerSize];
        [self constructSubviews];
    }
    return self;
}

- (void)configerBannerSize {
    self.bigBannerHeight = self.frame.size.height; //self.frame.size.width / 1300.0 * 538.0;
    self.bannerGap = 5;
    //    CGRect frame = self.frame;
    //    frame.size.height = 120; //self.bigBannerHeight + self.bannerGap + self.smallBannerHeight;
    //    self.frame = frame;
}

- (void)constructSubviews {
    [self addSubview:self.bigBanner];
    [self addSubview:self.bigBannerPageControl];
}

- (void)layoutWithBigBanners:(NSArray *)bigBanners smallBanners:(NSArray *)smallBanners {
    //    self.bigBannerPageControl.height = 30;
    
    self.bigPromotions = bigBanners;
    [self.bigBanner reloadData];
    [self.bigBannerPageControl reloadData];
}

#pragma mark - 懒加载
- (iCarousel *)bigBanner {
    if (!_bigBanner) {
        _bigBanner = [[iCarousel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.bigBannerHeight)];
        _bigBanner.clipsToBounds = YES;
        _bigBanner.delegate = self;
        _bigBanner.dataSource = self;
        _bigBanner.type = iCarouselTypeCylinder;
        _bigBanner.pagingEnabled = YES;
        
        __weak typeof(self) weakSelf = self;
        __weak typeof(_bigBanner) weakBigBanner = _bigBanner;
        //        //使用计时器进行轮播
        //        [NSTimer mq_scheduledTimerWithTimeInterval:3 repeats:YES handlerBlock:^BOOL{
        //            if (!weakSelf) {
        //                return NO;
        //            }
        //            [weakBigBanner scrollToItemAtIndex:weakBigBanner.currentItemIndex + 1 animated:YES];
        //
        //            return YES;
        //        }];
    }
    return _bigBanner;
}

- (DDHomeBannerPageControlView *)bigBannerPageControl {
    if (!_bigBannerPageControl) {
        CGFloat height = 30;
        CGFloat width = self.frame.size.width;
        _bigBannerPageControl = [[DDHomeBannerPageControlView alloc] initWithFrame:CGRectMake(0, self.bigBannerHeight - height, width, height)];
        _bigBannerPageControl.dataSource = self;
        _bigBannerPageControl.selectedColor = [UIColor whiteColor];
        _bigBannerPageControl.noSelectedColor = [UIColor lightGrayColor];
    }
    return _bigBannerPageControl;
}

#pragma mark - iCarouselDelegate, iCarouselDataSource
- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel {
    return self.bigPromotions.count;
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(nullable UIImageView *)view {
    if (!view) {
        view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.bigBannerHeight)];
        view.contentMode = UIViewContentModeScaleAspectFill;
        view.clipsToBounds = YES;
    }
    NSString *promotion = [self.bigPromotions objectAtIndex:index];
    NSString *imageUrl = promotion;
    if ([promotion hasPrefix:@"http"]) {
        [view sd_setImageWithURL:[NSURL URLWithString:imageUrl]];
    } else {
        view.image = [UIImage imageNamed:promotion];
    }
//    [view sd_setImageWithURL:[NSURL URLWithString:imageUrl]];
    //    [view showScaleImageWithUrl:imageUrl urlSuffix:@"" placeholder:[UIImage imageNamed:@"ic_picture_loadeed"] cornerRadius:0];
    return view;
}

- (void)carouselDidEndScrollingAnimation:(iCarousel *)carousel {
    //在此处理小圆点选中位置
    [self.bigBannerPageControl selectAtIndex:self.bigBanner.currentItemIndex];
}

- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index {
    //选择了某一行
    NSString *promotion = [self.bigPromotions objectAtIndex:index];
    if ([self.delegate respondsToSelector:@selector(homeRecommendBannerView:didSelectPromotion:atIndexPath:)]) {
        [self.delegate homeRecommendBannerView:self didSelectPromotion:promotion atIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
    }
}


#pragma mark DDHomeBannerPageControlViewDataSource
- (NSInteger)numberOfItemInPageControl:(DDHomeBannerPageControlView *)pageControl {
    return self.bigPromotions.count;
}

- (NSInteger)indexOfCurrentSelectItemInPageControl:(DDHomeBannerPageControlView *)pageControl {
    return self.bigBanner.currentItemIndex;
}

@end

